﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using DevExpress.LookAndFeel;
using DevExpress.Skins;
using DevExpress.XtraBars;
using WmsApp.Security;

namespace WmsApp.Style
{
    public interface ILookAndFeelController
    {
        void LoadLookAndFeelMenu(BarSubItem barSubItem, DefaultLookAndFeel lookAndFeel);
    }

    public class LookAndFeelController : ILookAndFeelController
    {
        private BarSubItem barSubItem;
        private DefaultLookAndFeel lookAndFeel;
        public void LoadLookAndFeelMenu(BarSubItem barSubItem, DefaultLookAndFeel lookAndFeel)
        {
            this.barSubItem = barSubItem;
            this.lookAndFeel = lookAndFeel;

            string originalStyle = ReadStyle();
            if (String.IsNullOrEmpty(originalStyle))
            {
                originalStyle = DevExpress.Skins.SkinManager.Default.Skins[6].SkinName;
            }

            this.lookAndFeel.LookAndFeel.SkinName = originalStyle;

            foreach (SkinContainer sc in DevExpress.Skins.SkinManager.Default.Skins)
            {
                var barCheckItem = new BarCheckItem(barSubItem.Manager, false);
                barCheckItem.Caption = sc.SkinName;
                if (sc.SkinName == originalStyle)
                {
                    barCheckItem.Checked = true;
                }
                barSubItem.AddItem(barCheckItem);
                barCheckItem.ItemClick += (object sender, ItemClickEventArgs e) =>
                                              {
                                                  WriteStyle(e.Item.Caption);
                                                  this.lookAndFeel.LookAndFeel.SkinName = e.Item.Caption;
                                                  ((BarCheckItem)e.Item).Checked = true;
                                                  foreach (var itemLink in barSubItem.ItemLinks)
                                                  {
                                                      var item = (BarCheckItem)(((BarCheckItemLink)itemLink).Item);
                                                      if (item != e.Item)
                                                      {
                                                          item.Checked = false;
                                                      }
                                                  }
                                              };
            }
        }

        private string GetPath()
        {
            return System.Windows.Forms.Application.StartupPath + "\\defaultStyle.style";
        }

        private void WriteStyle(string style)
        {
            string filePath = GetPath();
            var sw = new System.IO.StreamWriter(filePath, false, System.Text.Encoding.UTF8);
            try
            {
                sw.Write(style);
                sw.Flush();
            }
            catch { }
            finally
            {
                sw.Close();
            }
        }

        private string ReadStyle()
        {
            if (!File.Exists(GetPath()))
            {
                return String.Empty;
            }
            var sw = new System.IO.StreamReader(GetPath(), System.Text.Encoding.UTF8);
            try
            {
                string style = sw.ReadToEnd();
                return style;
            }
            catch { }
            finally
            {
                sw.Close();
            }
            return String.Empty;
        }
    }
}
